home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1129 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  64 lines

  1. Path: news.infi.net!usenet
  2. From: nngis@norfolk.infi.net (Greg DiGiorgio)
  3. Newsgroups: comp.lang.c,comp.lang.objective-c
  4. Subject: Re: Comma Delimited function wanted
  5. Date: 11 Jan 1996 16:13:51 GMT
  6. Organization: Customer of InfiNet
  7. Distribution: world
  8. Message-ID: <4d3cvv$fsm@news.infi.net>
  9. References: <4d1l42$mb6@voyager.Internex.NET>
  10. Reply-To: nngis@norfolk.infi.net
  11. NNTP-Posting-Host: h-langoliers.norfolk.infi.net
  12. Mime-Version: 1.0
  13. X-Newsreader: WinVN 0.99.3
  14.  
  15. In article <4d1l42$mb6@voyager.Internex.NET>, ian_stewart@nyro.com 
  16. says...
  17. >
  18. >Help with a comma delimited function needed.
  19. >
  20. >I did this once before, but I am blocked and can't
  21. >find the original source.
  22. >
  23. >Here is what I need to do:
  24. >
  25. >I have a buffer full of text.
  26. >
  27. >char buffer = "3740067099,914885AC2,P03,5000";
  28. >
  29. >I have four char vars to put this into.
  30. >
  31. >char field1[20], field2[20], field3[20], field4[20];
  32. >
  33. >What I want to do is get this as the end result.
  34. >
  35. >
  36. >field1 = 3740067099
  37. >field2 = 914885AC2
  38. >field3 = P03
  39. >field4 = 5000
  40. >
  41. >
  42. >Anyway, help is appreciated.
  43. >
  44. >ian
  45.  
  46. Ian, here's your answer. The key is the "strtok" function. 
  47.  
  48. #include <string.h>
  49. #include <stdio.h>
  50. void main (void) {
  51.    char buf[]={"12345,67890,12345"};
  52.    char *s;
  53.  
  54.    s=strtok(buf,","); /* First call to strtok, pass entire buffer */
  55.    while (s) {        /* While strtok returns a non-NULL value, loop */
  56.       printf("%s\n",s); /* display parsed value */
  57.       s=strtok(NULL,","); /* Get next value - notice the NULL */
  58.    }
  59.  
  60. }
  61.  
  62.  
  63.  
  64.